// _form/element/radio/index.js
const { $http, $config, c0 } = getApp()
Component({
    behaviors: ['wx://form-field-group'],
    options:{
        virtualHost: true
    },
    properties: {
        data: null,
        name: String,
        value: null,
        api: null,
        param: null,
        idStr: null,
        nameStr: null,
        disabled: Boolean,
        color: null,
        t:Boolean,         // 数据类型
        fontSize:Number,
        str:Boolean
    },
    data: {
        c0,
        $config,
        rows: []
    },
    lifetimes:{
        attached(){
            const { data, api, t } = this.data
            if(api){
                $http.pull(null, api, { param }).then(rows=>{
                    his.setData({ rows })
                })
            }else{
                let value1 = '1'
                let value2 = '0'
                let rows = []
                if( this.data.name === 'sex' ){
                    rows = [
                        { label:'男', value: 1 },
                        { label:'女', value: 2 }
                    ]
                    value1 = 1
                    value2 = 2
                }else{
                    if( t ){
                        value1 = true
                        value2 = false
                    }else{
                        value1 = 1
                        value2 = 0
                    }
                    rows = [
                        { label:'是', value: value1 },
                        { label:'否', value: value2 }
                    ]
                }
                rows = Array.isArray(data) ? data : rows
                this.setData({ rows })
            }
        }
    },
    methods: {
        change({ detail: { value } }){
            const { t, str } = this.data
            if( !str ){
                if( t ) value = Boolean(value)
                else value = Number(value)
            }
            this.setData({ value })
            this.triggerEvent('change', value)
        }
    }
})
